//
//  DataModel.m
//  2048
//
//  Created by 高新川 on 15/6/7.
//  Copyright (c) 2015年 高新川. All rights reserved.
//

#import "DataModel.h"

@implementation DataModel

- (id)init {
    self = [super init];
    if (self) {
        _matrix = 4;
    }
    return self;
}

- (NSNumber *)objectAt:(int)x and:(int)y {
    NSNumber *number = [NSNumber numberWithInt:data[x][y]];
    return number;
}

- (NSString *)objectAtToString:(int)x and:(int)y {
    NSNumber *number = [self objectAt:x and:y];
    NSNumberFormatter *format = [[NSNumberFormatter alloc] init];
    NSString *str = [format stringFromNumber:number];
    return str;
}

- (array)getData {
    return data;
}

- (void)initNumbers {
    for (int i = 0;  i < _matrix; i++) {
        for (int j = 0; j < _matrix; j++) {
            data[i][j] = 0;
        }
    }
    // 初始化开始游戏时两个数据的随机位置
    int t = 2;
    while (t > 0) {
        int x = arc4random() % _matrix;
        int y = arc4random() % _matrix;
        if (data[x][y] == 0) {
            data[x][y] = 2;
            t--;
        }
    }
}

- (void)output {
    for (int i = 0; i < _matrix; i++) {
        NSLog(@"%i %i %i %i",data[i][0],data[i][1],data[i][2],data[i][3]);
    }
}

@end
